package com.dmap.run.controller.api;

import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import com.dmap.run.bo.RidingRecordSimple;
import com.dmap.run.bo.RunRecordSimple;
import com.dmap.run.constants.RunConstans;
import com.dmap.run.controller.api.param.RidingRecordParam;
import com.dmap.run.po.RidingRecord;
import com.dmap.run.po.RunRecord;
import com.dmap.run.po.mongo.MapTrack;
import com.dmap.run.service.MapTrackService;
import com.dmap.run.service.RidingRecordService;
import com.dmap.run.vo.RidingRecordDetailVo;
import com.dmap.run.vo.RidingRecordHistoryVo;
import com.dmap.run.vo.RunRecordDetailVo;
import com.dmap.run.vo.RunRecordHistoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * \* User: silent
 * \* Date: 08/06/18 Time: 上午 10:14
 * \* Description:
 * \
 */
@Api(value = "骑行相关接口", description = "骑行相关接口")
@Controller
@RequestMapping(value = "/api/dmap-run-server/v1/riding")
public class RidingRecordController {

    @Resource
    private RidingRecordService ridingRecordService;
    @Resource
    private MapTrackService mapTrackService;



    @ResponseBody
    @ApiOperation(value = "骑行相关接口", notes = "骑行相关接口")
    @RequestMapping(value = "/accept/riding/data", method = RequestMethod.POST)
    public BaseVo accpectRunRecord(@RequestBody RidingRecordParam param) {
        BaseVo baseVo = new BaseVo();
        if (param.getUserId() == null || param.getType() == null
                || param.getDataSource() == null || param.getStartTime() == null
                || param.getEndTime() == null || param.getDistanceTotal() == null
                || param.getClimbingDistance() == null || param.getCalorie() == null
                 || param.getTimeTotal() == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        if (ridingRecordService.isExistRidingRecord(param.getUserId(), param.getStartTime(), param.getEndTime())) {
            baseVo.setCode(RunConstans.RESPONSE_IS_EXIST_THIS_RECORD_CODE);
            baseVo.setMsg(RunConstans.RESPONSE_IS_EXIST_THIS_RECORD_MSG);
            return baseVo;
        }
        boolean isSuccess = ridingRecordService.insertRidingRecord(param);
        if (!isSuccess) {
            baseVo.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
            return baseVo;
        }
        if (CollectionUtils.isNotEmpty(param.getTrackList())) {
            MapTrack mapTrack = new MapTrack();
            mapTrack.setUserId(param.getUserId());
            mapTrack.setType(2);
            mapTrack.setDataSource(param.getDataSource());
            mapTrack.setStartTime(param.getStartTime());
            mapTrack.setEndTime(param.getEndTime());
            mapTrack.setTrackList(param.getTrackList());
            mapTrackService.insertMapTrack(mapTrack);
        }
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "获取用户骑行历史总公里数", notes = "获取用户历史跑步总公里数")
    @RequestMapping(value = "/get/user/total/distance", method = RequestMethod.GET)
    public BaseVo getUserDistanceTotal(@RequestParam Long userId) {
        BaseVo baseVo = new BaseVo();
        if (userId == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        int totalDistance = ridingRecordService.selectUserTotalDistance(userId);
        Map<String, Integer> map = new HashMap<>();
        map.put("totalDistance", totalDistance);
        baseVo.setData(map);
        return baseVo;
    }


    @ResponseBody
    @ApiOperation(value = "获取骑行跑步数据列表", notes = "获取历史跑步数据列表")
    @RequestMapping(value = "/get/riding/data", method = RequestMethod.GET)
    public BaseVo getRunRecordList(@RequestParam Long userId, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
        BaseVo baseVo = new BaseVo();
        if (userId == null || pageIndex == null || pageSize == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        RidingRecordHistoryVo ridingRecordHistoryVo = ridingRecordService.selectRidingRecordHistoryList(userId);
        if (ridingRecordHistoryVo.getRidingNum() > 0) {
            List<RidingRecord> ridingRecords = ridingRecordService.selectRidingRecordListByPage(userId, pageIndex, pageSize);
            List<RidingRecordSimple> runRecordSimpleList = new ArrayList<>(pageSize);
            for (RidingRecord record : ridingRecords) {
                runRecordSimpleList.add(new RidingRecordSimple(record));
            }
            ridingRecordHistoryVo.setRidingRecordSimpleList(runRecordSimpleList);
        }
        baseVo.setData(ridingRecordHistoryVo);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "获取骑行数据详情", notes = "获取跑步数据详情")
    @RequestMapping(value = "/accept/riding/data/detail", method = RequestMethod.GET)
    public BaseVo getRunRecordDetail(@RequestParam Long ridingRecordId) {
        BaseVo baseVo = new BaseVo();
        if (ridingRecordId == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        RidingRecord ridingRecord = ridingRecordService.selectRidingRecordById(ridingRecordId);
        //没有查到返回错误码
        if (ridingRecord == null) {
            baseVo.setCode(RunConstans.RESPONSE_NOT_EXIST_THIS_RECORD_CODE);
            baseVo.setMsg(RunConstans.RESPONSE_NOT_EXIST_THIS_RECORD_MSG);
            return baseVo;
        }
        RidingRecordDetailVo ridingRecordDetailVo = new RidingRecordDetailVo(ridingRecord);
        //获取地图轨迹数据
        MapTrack mapTrack = mapTrackService.selectMapTrackByUserIdAndStartTimeAndEndTime(ridingRecord.getUserId(), ridingRecord.getStartTime(), ridingRecord.getEndTime());
        if (mapTrack != null) {
            ridingRecordDetailVo.setTrackList(mapTrack.getTrackList());
        } else {
            //没有地图轨迹放回一个空集合
            ridingRecordDetailVo.setTrackList(Collections.emptyList());
        }
        baseVo.setData(ridingRecordDetailVo);
        return baseVo;
    }
}
